package DAL;

import Entities.TblSemester;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class SemesterDAL {

    private ResultSet objResult;
    public List<TblSemester> list;

    public SemesterDAL() {
    }

    public boolean add(TblSemester semester) {
        int result = 0;
        String[] params = new String[2];
        params[0] = String.valueOf(semester.getCurriculumId());
        params[1] = semester.getSemesterName();

        try {
            result = SQLHelper.executeUpdate("procInsertSemester", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblSemester semester) {
        int result = 0;
        String[] params = new String[3];
        params[0] = String.valueOf(semester.getSemesterId());
        params[1] = String.valueOf(semester.getCurriculumId());
        params[2] = semester.getSemesterName();

        try {
            result = SQLHelper.executeUpdate("procUpdateSemester", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblSemester semester) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(semester.getSemesterId());

        try {
            result = SQLHelper.executeUpdate("procDeleteSemester", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblSemester> getAll() {
        list = new ArrayList<TblSemester>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllSemester", null);
            while (objResult.next()) {
                TblSemester semester = new TblSemester();
                semester.setSemesterId(objResult.getInt("SemesterId"));
                semester.setCurriculumId(objResult.getInt("CurriculumId"));
                semester.setSemesterName(objResult.getString("SemesterName"));
                semester.setIsDelete(objResult.getBoolean("IsDelete"));
                list.add(semester);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblSemester getById(int id) {
        TblSemester semester = new TblSemester();
        String[] params = new String[1];
        params[0] = String.valueOf(id);

        try {
            objResult = SQLHelper.executeQuery("procGetSemesterById", params);
            while (objResult.next()) {
                semester.setSemesterId(objResult.getInt("SemesterId"));
                semester.setCurriculumId(objResult.getInt("CurriculumId"));
                semester.setSemesterName(objResult.getString("SemesterName"));
                semester.setIsDelete(objResult.getBoolean("IsDelete"));
            }

            return semester;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblSemester> getByCurriculumId(int curriculumId) {
        list = new ArrayList<TblSemester>();
        String[] params = new String[1];
        params[0] = String.valueOf(curriculumId);
        try {
            objResult = SQLHelper.executeQuery("procGetSemesterByCurriculumId", params);
            while (objResult.next()) {
                TblSemester semester = new TblSemester();
                semester.setSemesterId(objResult.getInt("SemesterId"));
                semester.setCurriculumId(objResult.getInt("CurriculumId"));
                semester.setSemesterName(objResult.getString("SemesterName"));
                semester.setIsDelete(objResult.getBoolean("IsDelete"));
                list.add(semester);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
